<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        #div1{
            width: 200px;
            height: 200px;
            background: red;
            position: absolute;
        }
    </style>
    <script>
        function getPos(ev){
            var scrollTop=document.documentElement.scrollTop||document.body.scrollTop;
            var scrollLeft=document.documentElement.scrollLeft||document.body.scrollLeft;

            return{x: ev.clientX+scrollLeft, y: ev.clientY+scrollTop};
        }
        document.onmousemove=function(ev){
            var oEevnt=ev||event;
            var oDiv=document.getElementById('div1');
            //var scrollTop=document.documentElement.scrollTop||document.body.scrollTop;
            //封装成getPos()函数
            //用法：
            var pos=getPos(oEevnt);
            oDiv.style.left=pos.x+'px';
            oDiv.style.top=pos.y+'px';
            //oDiv.style.left=oEevnt.clientX+'px';
            //oDiv.style.top=oEevnt.clientY+scrollTop+'px';

            /*clientX和 clientY是指在页面可视区的鼠标位置。
            当我向下滑动页面时，可视区变化了，比如

            old 的可视区clientX: 100; clientY: 100
            new 的可视区clientX: 100; clientY: 100
            
            */
        }
    </script>
</head>
<body style="height: 2000px">
<div id="div1"></div>    
</body>
</html>